package com.alipaydemo.pay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipaydemo.pay.pojo.AlipayOrder;
import com.alipaydemo.pay.pojo.OrderVo;
import com.alipaydemo.pay.utils.Result;
import com.alipaydemo.pay.utils.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @date 2023/4/19
 */
@Component
public class PayUtils {


    @Value("${alipay.appid}")
    private  String appid;
    @Value("${alipay.publicKey}")
    private String publicKey;
    @Value("${alipay.privateKey}")
    private String privateKey;
    @Value("${alipay.url}")
    private String url;
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;
    @Value("${alipay.returnUrl}")
    private String returnUrl;


    public Result createOrder(OrderVo ordersVo){
        AlipayClient alipayClient=new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(returnUrl);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", ordersVo.getOrderNo());
        bizContent.put("total_amount", ordersVo.getPrice());
        bizContent.put("subject", ordersVo.getDescribe());
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
        String form = null;
        try {
            response = alipayClient.pageExecute(request);
            form = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return Result.success(form);
    }

    /**
     * 根据外部订单号查询订单支付详情
     * @param outTradeNo
     * @return
     * @throws AlipayApiException
     */
    public Result queryOrderFromAli(String outTradeNo) throws AlipayApiException {
        AlipayClient alipayClient=new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no",outTradeNo);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                AlipayOrder alipayOrderVo=new AlipayOrder();
                BeanUtils.copyProperties(response,alipayOrderVo);
                if("TRADE_SUCCESS".equals(alipayOrderVo.getTradeStatus())){
                    //成功后的事件
                    return Result.success();
                }else{
                    return Result.builder(ResultEnum.FAILED).build();
                }
            }
        return Result.builder(ResultEnum.FAILED).build();
    }
}
